package com.itheima.service.store.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.store.QuestionDao;
import com.itheima.dao.store.QuestionItemDao;
import com.itheima.domain.store.Question;
import com.itheima.domain.store.QuestionItem;

import com.itheima.factory.MapperFactory;
import com.itheima.service.store.QuestionItemService;
import com.itheima.service.store.QuestionService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.UUID;

/**
 * @program: Web综合案例
 * @author: DingYaDong
 * @create: 2021-06-25 10:56
 */
public class QuestionItemServiceImpl implements QuestionItemService {

    @Override
public String save(QuestionItem questionItem,boolean flag) {
    SqlSession sqlSession = null;
    try {
        sqlSession = MapperFactory.getSqlSession();
        QuestionItemDao questionItemDao = MapperFactory.getMapper(sqlSession, QuestionItemDao.class);
        String uuid = UUID.randomUUID().toString();
        questionItem.setId(uuid);

        if (flag){
            //设置当前存储的图片名称为id值
            questionItem.setPicture(uuid);
        }
        System.out.println("dao层的save方法执行了"+questionItem);
        questionItemDao.save(questionItem);
        //提交事务
        TransactionUtil.commit(sqlSession);
        return uuid;
    } catch (Exception e) {

        TransactionUtil.rollback(sqlSession);
        throw new RuntimeException(e);
        //记录日志
    } finally {
        try {
            TransactionUtil.close(sqlSession);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

    @Override
    public int delete(QuestionItem questionItem) {
        int del = 0;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            QuestionItemDao QuestionItemDao = MapperFactory.getMapper(sqlSession, QuestionItemDao.class);
            del = QuestionItemDao.delete(questionItem);
            //提交事务
            TransactionUtil.commit(sqlSession);
            return del;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public int update(QuestionItem questionItem) {
        int up = 0;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            QuestionItemDao QuestionItemDao = MapperFactory.getMapper(sqlSession, QuestionItemDao.class);
            up = QuestionItemDao.update(questionItem);
            //提交事务
            TransactionUtil.commit(sqlSession);
            return up;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }



    @Override
    public QuestionItem findById(String id) {
        QuestionItem byId = null;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            QuestionItemDao QuestionItemDao = MapperFactory.getMapper(sqlSession, QuestionItemDao.class);
            byId = QuestionItemDao.findById(id);
            return byId;
        } catch (Exception e) {

            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public PageInfo findAll(String questionId,int page,int size) {
        List<QuestionItem> questions;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            QuestionItemDao questionItemDao = MapperFactory.getMapper(sqlSession, QuestionItemDao.class);
            //PageHelper设置分页必须出现在查询的前面
            PageHelper.startPage(1,10);
            //查询
            questions = questionItemDao.findAll(questionId);
            PageInfo pageInfo = new PageInfo(questions);
            return pageInfo;
        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }




}